package com.goal98.grails.plugin.account.controllers

import com.goal98.grails.plugin.account.domain.Credit
import com.goal98.grails.plugin.account.domain.Account

class CreditController {
    
    def index = { redirect(action:list,params:params) }
    def accountService

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ creditInstanceList: Credit.list( params ), creditInstanceTotal: Credit.count() ]
    }

    def show = {
        def creditInstance = Credit.get( params.id )

        if(!creditInstance) {
            flash.message = "Credit not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ creditInstance : creditInstance ] }
    }

    def delete = {
        def creditInstance = Credit.get( params.id )
        if(creditInstance) {
            try {
                creditInstance.delete(flush:true)
                flash.message = "Credit ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Credit ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Credit not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def creditInstance = Credit.get( params.id )

        if(!creditInstance) {
            flash.message = "Credit not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ creditInstance : creditInstance ]
        }
    }

    def update = {
        def creditInstance = Credit.get( params.id )
        if(creditInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(creditInstance.version > version) {
                    
                    creditInstance.errors.rejectValue("version", "credit.optimistic.locking.failure", "Another user has updated this Credit while you were editing.")
                    render(view:'edit',model:[creditInstance:creditInstance])
                    return
                }
            }
            creditInstance.properties = params
            if(!creditInstance.hasErrors() && creditInstance.save()) {
                flash.message = "Credit ${params.id} updated"
                redirect(action:show,id:creditInstance.id)
            }
            else {
                render(view:'edit',model:[creditInstance:creditInstance])
            }
        }
        else {
            flash.message = "Credit not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def creditInstance = new Credit()
        creditInstance.properties = params
        return ['creditInstance':creditInstance]
    }

    def save = {
        def creditInstance = new Credit(params)
        if(!creditInstance.hasErrors() && creditInstance.save()) {
            flash.message = "Credit ${creditInstance.id} created"
            redirect(action:show,id:creditInstance.id)
        }
        else {
            render(view:'create',model:[creditInstance:creditInstance])
        }
    }

    def creditAccount = {
        def creditInstance = new Credit(params)
        def account = Account.get(params."account.id")
        def amount = creditInstance.amount
        accountService.credit(account, amount)
        redirect(controller:'account',action:show,id:params."account.id")

    }
}
